<?php
namespace app\admin\controller;
use app\api\model\Goods;
use app\api\model\PointsRecord;
use app\api\model\User;
use think\Controller;
use think\Db;
use think\Exception;
use think\Paginator;
use app\admin\model\UserModel;
use think\Loader;
use app\admin\model\OrderModel;
use app\admin\model\GoodsModel;
use think\Request;

// 订单控制
class Order extends Common
{

    
    
    // 订单列表展示 分页筛选
    public function getOrderList(Request $request){
      
        $search_type_of_time=$request->param('search_type_of_time','');
        $atime=$request->param('atime','');
        $ztime=$request->param('ztime','');
        
        $status= $request->param('status', '0');

        if(!$atime || !$ztime){
            $search_type_of_time='';
            $atime='';
            $ztime='';
        }
        if($search_type_of_time ==''){
            $atime = '';
            $ztime = '';
        }

        $keyword=$request->param('keyword','');
        $order_type=$request->param('order_type',1);

        
        $list =  OrderModel::orderList($keyword,$order_type, $search_type_of_time, $atime, $ztime ,$status);

        
        $this->assign([
            'list'=>$list->paginate(),
            'keyword'=>$keyword,
            'order_type'=>$order_type,
            'search_type_of_time'=> $search_type_of_time,
            'atime'=>$atime,
            'ztime'=>$ztime,
            'status'=>$status,
        ]);

        return $this->fetch('order/orderList');
    }

    // 更改 订单状态
    public function postAjaxChangeStatus(Request $request)
    {
        $info_id = $request->post('id');
        $status = $request->post('status');
        if(!is_numeric($info_id))
        {
            die(json_encode(['status'=>false,'msg'=>'参数错误']));
        }
        
       
        Db::startTrans();
        try{
            $orig_status = OrderModel::getStatusForUpdate($info_id);
            if(empty($orig_status))
            {
                throw  new Exception('没有该订单信息');
            }
            if($orig_status['status'] == 4 )
            {
                throw  new Exception('该订单已完成，不能修改');
            }
            if($orig_status['status'] == 5)
            {
                throw  new Exception('该订单已取消，不能修改');
            } 
            
            switch ($status)
            {
                case OrderModel::STATUS_PAY:
                    OrderModel::updatePayStatus($info_id);
                    break;
                case OrderModel::STATUS_DELIVERY:
                    OrderModel::updateDeliveryStatus($info_id);
                    break;
                case OrderModel::STATUS_COMPLETE:
                    OrderModel::updateCompleteStatus($info_id);
                    break;
                case OrderModel::STATUS_CANCEL:
                    GoodsModel::incrGoodsNum($orig_status['goods_id'],$orig_status['num']);
                    User::descUserPoints($orig_status['user_id'],Goods::getGoodsPoints($orig_status['goods_id']));
                    PointsRecord::addRecord($orig_status['user_id'],PointsRecord::POINTS_ORDER_CANCEL,$info_id,Goods::getGoodsPoints($orig_status['goods_id']));
                    OrderModel::updateCancelStatus($info_id);
                    break;
                default:
                    break;
            }
        }catch (Exception $e)
        {
            Db::rollback();
            die(json_encode(['status'=>false,'msg'=>$e->getMessage()]));
        }
        
        Db::commit();
        die(json_encode(['status'=>true,'msg'=>'']));
        
    }

    // 订单详情
    public function getOrderInfo(){
        $orderModel = new \app\admin\model\OrderModel;
        $userModel =new \app\admin\model\UserModel();


        $order_id =input('id');

        $orderInfo=$orderModel->getOrder($order_id);

            // 绑定用户信息
        $user_name= $userModel->getUserInfo($orderInfo['user_id'],'user_name');
        $user_name=$user_name['user_name'];

        $addressInfo= $userModel->getAddress($orderInfo['address_id'], 'receiver_name,receiver_phone,address,house_number');

        // $addressInfo=$addressInfo['address'];

        $orderInfoList=$orderModel->getOrderInfoList($orderInfo['order_info_id']);

        $goodsModel = new \app\admin\model\GoodsModel;

        foreach ($orderInfoList as $key => $value) {
            $goods_name = $goodsModel->getGoodsInfoById($value['goods_id'],'goods_name');
            $orderInfoList[$key]['goods_name']=$goods_name['goods_name'];
            $orderInfoList[$key]['amount']=$value['price']* $value['num'];

        }
        

        $this->assign([
            'list'=> $orderInfoList,
            'user_name'=> $user_name,
            'addressInfo'=> $addressInfo,
            'total'=> $orderInfo['actual_amount'],
        ]);

        return $this->fetch('order/orderInfoList');

    }

    // 导出订单信息到Excel
    /* 
        order 订单Excel 单独设置导出方法
    */
    public function getExcel(){

       $res= \app\admin\model\OrderModel::getOrderExcel();
    }

    // Ajax发货确认按钮
    public function postAjaxSendConfirm(){
        if(request()->isAjax()){
            $order_id=input('order_id');

            $orderModel = new \app\admin\model\OrderModel();
            $res=$orderModel->deliveryGoods($order_id);

            return $res;


        }
    }

    // 未发货订单列表
    public function getUnsendOrderList()
    {
        $request = request();

        $search_type_of_time = $request->param('search_type_of_time', '');
        $atime = $request->param('atime', '');
        $ztime = $request->param('ztime', '');


        if (!$atime || !$ztime) {
            $search_type_of_time = '';
            $atime = '';
            $ztime = '';
        }
        if ($search_type_of_time == '') {
            $atime = '';
            $ztime = '';
        }

        $keyword = $request->param('keyword', '');
        $order_type = $request->param('order_type', 1);

        $status = input('status', 2);

        $this->orderList($keyword, $order_type, $search_type_of_time, $atime, $ztime, $status);

        $this->assign([
            'keyword' => $keyword,
            'order_type' => $order_type,
            'search_type_of_time' => $search_type_of_time,
            'atime' => $atime,
            'ztime' => $ztime,

        ]);

        return $this->fetch('order/unsendOrderList');
    }

    // ajax 订单批量确认
    public function postOrderGroupComfirm(){
        if(request()->isAjax()){
            $ids=input('post.ids');

            $ids=explode(',',$ids);
            unset($ids[count($ids)-1]);

            $orderModel = new \app\admin\model\OrderModel();
            $res=$orderModel->orderGroupDel($ids);

            return $res;

        }
    }


}
